﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using Sinacor.Infra.Service.Data;
using Sinacor.Infra.Service.Security.Authorization;

namespace Sinacor.Servico.Bovespa.Contabil.ResourceAccess
{
    /// <summary>
    /// ResourceAccess Class - PlanoConta
    /// </summary>
    public class PlanoConta : BaseResourceAccess
    {
        #region Construtor

        /// <summary>
        /// Construtor
        /// </summary>
        public PlanoConta() : base(AuthorizationContext.Current.CompanyId) { }

        #endregion

        #region Métodos Privados

        #region CondicaoSql

        /// <summary>
        /// Método utilizado nas montagens de filtros opcionais
        /// </summary>
        /// <param name="possuiFiltro">Boolean</param>
        /// <returns>String</returns>
        private String CondicaoSql(Boolean possuiFiltro)
        {
            if (possuiFiltro)
                return " AND ";
            else
                return " WHERE ";
        }

        #endregion

        #endregion

        #region Métodos Públicos

        #region ConsultarPlanosContas

        /// <summary>
        /// Consulta planos de contas
        /// </summary>
        /// <param name="parameter">BusinessEntity.Parameter.FiltroPlanoConta</param>
        /// <returns>Lista de objetos do tipo BusinessEntity.PlanoConta</returns>
        public List<BusinessEntity.PlanoConta> ConsultarPlanosContas(BusinessEntity.Parameter.FiltroPlanoConta parameter)
        {
            List<BusinessEntity.PlanoConta> response = new List<BusinessEntity.PlanoConta>();

            using (DbCommand dbCommand = this.DataBase.GetSqlStringCommand(" "))
            {
                String condicao = String.Empty;
                Boolean possuiFiltro = false;

                if (parameter.CodigoResumidoConta.HasValue)
                {
                    this.DataBase.AddInParameter(dbCommand, "CodigoResumidoConta", DbType.Int32, parameter.CodigoResumidoConta.Value);
                    condicao = " WHERE COD_RESU_CON = :CodigoResumidoConta ";
                    possuiFiltro = true;
                }

                if (parameter.DigitoResumidoConta.HasValue)
                {
                    this.DataBase.AddInParameter(dbCommand, "DigitoResumidoConta", DbType.Byte, parameter.DigitoResumidoConta.Value);
                    condicao += this.CondicaoSql(possuiFiltro) + " DIG_CON = :DigitoResumidoConta ";
                    possuiFiltro = true;
                }

                if (!String.IsNullOrEmpty(parameter.CodigoConta))
                {
                    this.DataBase.AddInParameter(dbCommand, "CodigoConta", DbType.String, parameter.CodigoConta);
                    condicao += this.CondicaoSql(possuiFiltro) + " COD_CCTB_FORT = :CodigoConta ";
                }
                
                String sql = String.Format(@"SELECT COD_RESU_CON CodigoResumidoConta,
                                                    DIG_CON DigitoResumidoConta,
                                                    COD_CCTB_FORT CodigoConta,
                                                    NOME_CON NomeConta
                                               FROM TCTCAD_PLA_CON
                                                {0}", condicao);

                dbCommand.CommandText = sql;

                response = this.FindAll<BusinessEntity.PlanoConta>(dbCommand,
                    reader =>
                    {
                        BusinessEntity.PlanoConta entity = new BusinessEntity.PlanoConta();

                        entity.CodigoResumidoConta = reader.GetValue<Int32>("CodigoResumidoConta");
                        entity.DigitoResumidoConta = reader.GetValue<Byte>("DigitoResumidoConta");
                        entity.CodigoConta = reader.GetValue<String>("CodigoConta");
                        entity.NomeConta = reader.GetValue<String>("NomeConta");
                       
                        return entity;
                    });

                return response;
            }
        }

        #endregion

        #endregion
    }
}
